$(function() {
	layui.use(['form', 'laydate', 'layer','util'], function() {
		var form=layui.form;
		var layer = layui.layer;
		var laydate=layui.laydate;
		var util=layui.util;
		form.render(); 
		//重新加载date空间
		laydate.render({
			elem: "#birthday", //绑定文本框的id
			type: "date" //时间类型，年月日，时分秒
		});
		//截取地址栏中的字符串
		function GetQueryString(name) {
			var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
			var r = window.location.search.substr(1).match(reg);
			if(r != null) return unescape(r[2]);
			return null;
		}
		BaseUtil.ajaxSetup();
		var userId=BaseUtil.GetQueryString("userId");
		var version=null;
		//console.log(userId);
		
		//获取用户所拥有的角色列表
		var listUserRole=[];
		
		//通过用户ID查询用户要求
		function find_userInfo_ByUserId(userId){
			$.ajax({
				type:"get",
				url:"http://localhost:8080/ssmService/sysUser/selectUserVoByUserId/"+userId,
				async:false,
				"success": function(result) {
					if(result.resultCode == 0) {
						build_userInfo(result);
						listUserRole=result.data.listUserRole;
						version=result.data.user.version;
					} else {
						layer.msg(result.message, {
							icon: 2,
							time: 1000
						});
					}

				}
			});
		}
		function build_userInfo(result){
			var data=result.data;
			if(data.user.head_img_url!=""){
				$("#showImg").attr("src",data.user.head_img_url);
			}
			$("input[name='user.account']").val(data.user.account);
			$("input[name='user.name']").val(data.user.name);
			$("input[name='user.phone']").val(data.user.phone);
			var sel=["input[title='保密']","input[title='男']","input[title='女']"];
			$("input[name='user.birthday']").val(util.toDateString(data.user.birthday,"yyyy-MM-dd"));
			
			form.render();
		}
		//查找角色信息
		function find_roleName_list() {
			$.ajax({
				type: "get",
				url: "http://localhost:8080/ssmService/role/selectRoleName",
				async: false,
				"success": function(result) {
					if(result.resultCode == 0) {
						build_roleName_List(result,listUserRole);
					} else {
						layer.msg(result.message, {
							icon: 2,
							time: 1000
						});
					}

				}
			});
		}
		BaseUtil.ajaxSetup();
		function build_menu_list(result) {
			var menuList = result.data;
			$.each(menuList, function(index,item) {
				var parentDiv=$("<div></div>").addClass("layui-form-item");
				var smDiv=$("<div></div>").addClass("layui-input-block");
				var parentCheckBox=$("<input type='checkbox' class='parentCheckBox' lay-filter='parentCheckBox' value='"+item.code+"' title='"+item.name+"'/>");
				smDiv.append(parentCheckBox);
				var smCheckBoxAttr=item.roleValue.split(",");
				for(var i=0;i<smCheckBoxAttr.length;i++){
					var smCheckBox=$("<input type='checkbox' class='childrenCheckBox' lay-filter='childrenCheckBox' title='"+smCheckBoxAttr[i]+"'/>");
					smDiv.append(smCheckBox);
				}
				parentDiv.append(smDiv);
				$(".menu_list").append(parentDiv);
			});
			form.render();
		}
		
		
		function build_roleName_List(result,listUserRole) {
			var roleNameList = result.data;
			$.each(roleNameList, function(index, item) {
				var roleCheckBox = $("<input type='checkbox' value='" + item.role_id + "' title='" + item.name + "'>");
				$(".roleList").append(roleCheckBox);
				for(var i=0;i<listUserRole.length;i++){			
					if(item.role_id==listUserRole[i].fk_role_id){
						roleCheckBox.prop("checked",true);
					}
				}
			});
			form.render();
		}
		
		//将角色列表中选中的添加name以便传值到后台
		$(".updateUser").click(function(){
			check_role();
			var token=BaseUtil.uuid(16,16);
			$("#token").val(token);
			var user_id=parseInt(userId);
			$("#userId").val(userId);
			$("#version").val(version);
		});
		
		form.on("submit(updateUser)",function(data){
			console.log(data.field);
			var params=data.field;
			update_user(params);
			return false;//阻止跳转
		});
		//遍历角色列表，将选中的角色框添加name属性
		function check_role(){
			var i=0;
			$("input[type='checkbox']").each(function(){				
				if($(this).is(":checked")){
					$(this).attr("name","listUserRole["+i+"].fk_role+id");
					i++;
				}
			});
			form.render();
		}
		
		
		//修改用户的ajax
		function update_user(params){
			$.ajax({
				type:"post",
				url:"http://localhost:8080/ssmService/sysUser/updateUserVo",
				data:params,
				"success":function(result){
					if(result.resultCode == 0) {
						layer.msg(result.message, {
							icon: 1,
							time: 1000
						});
						var index=parent.layer.getFrameIndex(window.name);
						parent.location.reload();
						parent.layer.close(index);
				}
			}
		});
	}
		
		find_userInfo_ByUserId(userId);
		find_roleName_list();
	});
}); 
